﻿using System;
using System.Configuration;
using System.Reflection;
using SimpleServiceBus.Configuration;
using SimpleServiceBus.Endpoint;
using SimpleServiceBus.Endpoints.Msmq.Transport;
using SimpleServiceBus.Utilities.ObjectBuilder;

namespace SimpleServiceBus.Endpoints.Msmq.Configuration
{
    public class MsmqConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty("receiveQueueIsTransactional", IsRequired = false)]
        public bool ReceiveQueueIsTransactional
        {
            get { return (bool)base["receiveQueueIsTransactional"]; }
            set { base["receiveQueueIsTransactional"] = value; }
        }

        [ConfigurationProperty("destinationQueueIsTransactional", IsRequired = false)]
        public bool DestinationQueueIsTransactional
        {
            get { return (bool)base["destinationQueueIsTransactional"]; }
            set { base["destinationQueueIsTransactional"] = value; }
        }

        [ConfigurationProperty("attachSenderId", IsRequired = false)]
        public bool AttachSenderId
        {
            get { return (bool)base["attachSenderId"]; }
            set { base["attachSenderId"] = value; }
        }

        [ConfigurationProperty("purgeOnStartup", IsRequired = true)]
        public bool PurgeOnStartup
        {
            get { return (bool)base["purgeOnStartup"]; }
            set { base["purgeOnStartup"] = value; }
        }

        [ConfigurationProperty("inputQueue",IsRequired=false)]
        public string InputQueue
        {
            get { return (string) base["inputQueue"]; }
            set { base["inputQueue"] = value; }
        }
    }
    public class MsmqEndpointConfiguration : EndpointConfiguration
    {
        [ConfigurationProperty("msmq")]
        public MsmqConfigurationElement Msmq
        {
            get { return (MsmqConfigurationElement) base["msmq"]; }
        }


        protected override TransportType ConfigureTransport<TransportType>(IEndpointBuilder endpoint)
        {
            var transport = base.ConfigureTransport<TransportType>(endpoint) as MsmqTransport;

            if (transport != null)
            {
                transport.DestinationQueueIsTransactional = Msmq.DestinationQueueIsTransactional;
                transport.ReceiveQueueIsTransactional = Msmq.ReceiveQueueIsTransactional;
                transport.AttachSenderId = Msmq.AttachSenderId;
                transport.PurgeOnStartup = Msmq.PurgeOnStartup;
                if (!String.IsNullOrEmpty(Msmq.InputQueue))
                    transport.InputQueue = Msmq.InputQueue;
            }

            return transport as TransportType;
        }

        public static IMessagingEndpoint CreateConfiguredEndpoint(params Assembly[] messageHandlerAssemblies)
        {
            return CreateConfiguredEndpoint(null,messageHandlerAssemblies);
        }

        public static IMessagingEndpoint CreateConfiguredEndpoint(IObjectBuilder IoC,params Assembly[] messageHandlerAssemblies)
        {
            return CreateConfiguredEndpoint<MsmqEndpointBuilder<MessagingEndpoint>, MsmqTransport>(IoC,messageHandlerAssemblies);
            
        }

        public static IEndpointBuilder ConfigureEndpoint()
        {
            return ConfigureEndpoint(null);
        }

        public static IEndpointBuilder ConfigureEndpoint(IObjectBuilder IoC)
        {
            return ConfigureEndpoint<MsmqEndpointBuilder<MessagingEndpoint>, MsmqTransport>(IoC);
        }
    }
}
